package com.tjsinfo.tjsoa.system.bo;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.tjsinfo.tjsoa.Dao.IHibernateDao;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.system.dao.ITSystemPostDAO;
import com.tjsinfo.tjsoa.system.dao.ITSystemUserPostDAO;
import com.tjsinfo.tjsoa.system.vo.TSystemUserPost;

public class SystemUserPostBO implements ISystemUserPostBO{
	private IHibernateDao hibernateDao;
	private IQbcDao qbcDao;
	private ITSystemPostDAO systemPostDAO;
	private ITSystemUserPostDAO systemUserPostDAO;
	
	public IHibernateDao getHibernateDao() {
		return hibernateDao;
	}

	public void setHibernateDao(IHibernateDao hibernateDao) {
		this.hibernateDao = hibernateDao;
	}

	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}

	

	public ITSystemPostDAO getSystemPostDAO() {
		return systemPostDAO;
	}

	public void setSystemPostDAO(ITSystemPostDAO systemPostDAO) {
		this.systemPostDAO = systemPostDAO;
	}

	public ITSystemUserPostDAO getSystemUserPostDAO() {
		return systemUserPostDAO;
	}

	public void setSystemUserPostDAO(ITSystemUserPostDAO systemUserPostDAO) {
		this.systemUserPostDAO = systemUserPostDAO;
	}

	public Integer addSystemUserPost(TSystemUserPost systemUserPost) {
		// TODO Auto-generated method stub
		return hibernateDao.save(systemUserPost);
	}

	public Boolean deleteSystemUserPost(Integer id) {
		// TODO Auto-generated method stub
		try {
			TSystemUserPost systemUserPost = selectById(id);
			hibernateDao.delete(systemUserPost);
			return true;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	}

	public TSystemUserPost selectById(Integer id) {
		// TODO Auto-generated method stub
		return (TSystemUserPost) hibernateDao.findById(TSystemUserPost.class, id);
	}

	public List<TSystemUserPost> selectByUserId(Integer userid) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemUserPost.class);
		criteria.add(Restrictions.eq("userId", userid));
		List<TSystemUserPost> list = qbcDao.getCriteria(criteria);
		List<TSystemUserPost> list2 = new ArrayList<TSystemUserPost>();
		Integer orderid = 1;
		for (TSystemUserPost systemUserPost : list) {
			if(systemPostDAO.findById(systemUserPost.getPostId()) !=null)
			{
				systemUserPost.setPostName(systemPostDAO.findById(systemUserPost.getPostId()).getPostName());
				systemUserPost.setOrderNo(orderid);
				orderid++;
				list2.add(systemUserPost);
			}
			
		}
		return list2;
		
	}

	public Boolean updateSystemUserPost(TSystemUserPost systemUserPost) {
		// TODO Auto-generated method stub
		try {
			hibernateDao.update(systemUserPost);
			return true;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	public Boolean deleteSystemUserPostByUserId(Integer userid) {
		// TODO Auto-generated method stub
		try {
			List<TSystemUserPost> list = selectByUserId(userid);
			for (TSystemUserPost systemUserPost : list) {
				deleteSystemUserPost(systemUserPost.getId());
			}
			return true;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	}
	
}
